﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Draco.OWF.RT.Engine
{
    public class ShellCommand
    {
        public const String CMD_HELP = "help";
        public const String CMD_VERSION = "version";
        public const String CMD_LOAD_PROCESS = "loadprocess";
        public const String CMD_SHOW_JOBS = "showjobs";

        /// <summary>
        /// 
        /// </summary>
        public String Command{get;set;}
        /// <summary>
        /// 
        /// </summary>
        public IList<KeyValuePair<String, String>> Parameters { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="paraName"></param>
        /// <returns></returns>
        public String GetParameterValue(String paraName)
        {
            foreach (var pair in Parameters)
            {
                if (String.Compare(pair.Key, paraName, true) == 0)
                    return pair.Value;
            }
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public String[] ParameterNames
        {
            get
            {
                int i = 0;
                String[] names = new String[Parameters.Count];
                foreach (var pair in Parameters)
                {
                    names[i] = pair.Key;
                    i++;
                }
                return names;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public String DefaultParameter
        {
            get 
            {
                if (Parameters.Count > 0)
                    return Parameters[0].Key;
                return "";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmdString"></param>
        public ShellCommand(String cmdString)
        {
            this.Parameters = new List<KeyValuePair<String, String>>();

            if (String.IsNullOrWhiteSpace(cmdString)) return;

            String[] segs = cmdString.Split(new char[] {'-'}, StringSplitOptions.RemoveEmptyEntries);
            this.Command = segs[0].Trim();
            if (segs.Length > 1)
            {
                for (int i = 1; i < segs.Length; i++)
                {
                    String[] argSegs = segs[i].Split(new char[] { ' ' }, 2);
                    String val = argSegs.Length>1? argSegs[1]:"";
                    KeyValuePair<String, String> pair = new KeyValuePair<String, String>(argSegs[0], val);
                    Parameters.Add(pair);
                }
            }
        }
    }
}
